public class Board{
        //棋盘尺寸
        private int width;
        private int height;
        private Pieces pieces[][];

        public Board(int width, int height){
                if(width < 4 || height < 4){
                        this.width = 4;
                        this.height = 4;
                }else{
                        this.width = width;
                        this.height = height;
                }
                pieces= new Pieces[width][height];
                for(int i = 0; i < this.width; i++)
                        for(int j = 0; j < this.height; j++)
                                pieces[i][j] = Pieces.EMPTY;

        }
        private boolean valid_pos(Point pos){
                if(pos.x >= width || pos.y >= height)
                        return false;

                return true;
        }
        private boolean emptyPos(Point pos){
                return (getPos(pos) == Pieces.EMPTY);
        }
        private Pieces getPos(int x, int y){
                if(x >= width || y >= height)
                        return Pieces.NO;
                return pieces[x][y];
        }
        private Pieces getPos(Point pos){
                if(!valid_pos(pos))
                        return Pieces.NO;

                return pieces[pos.x][pos.y];
        }
        private  boolean  setPos(Point pos, Pieces p){
                if(!valid_pos(pos))
                        return false;
                pieces[pos.x][pos.y] = p;
                return true;
        }
        public boolean setPieces(Point pos, Pieces p){
                if(!emptyPos(pos))
                        return false;
                if(!setPos(pos, p))
                        return false;
                return true;
        }
        private boolean canMove(Point src_pos, Point dst_pos){
                if(src_pos.y > 0){
			System.out.println("can move up");
                        if(src_pos.y -1 == dst_pos.y && src_pos.x == dst_pos.x){
                                return true;
                        }
                }
                if(src_pos.x > 0){
                        if(src_pos.x -1 == dst_pos.x && src_pos.y == dst_pos.y){
                                return true;
                        }
                }
                if(src_pos.y < height -1){
                        if(src_pos.y + 1 == dst_pos.y && src_pos.x == dst_pos.x){
                                return true;
                        }
                }
                if(src_pos.x < height -1){
                        if(src_pos.x + 1 == dst_pos.x && src_pos.y == dst_pos.y){
                                return true;
                        }
                }
                return false;
        }
        public  boolean movePieces(Point src_pos, Point dst_pos, Pieces who){
                if(getPos(src_pos) != who)
                        return false;
                if(!emptyPos(dst_pos))
                        return false;

                if(!canMove(src_pos, dst_pos)){

                }
                setPos(src_pos, Pieces.EMPTY);
                setPos(dst_pos, who);
                return true;
        }
        public int getKills(Point src_pos, Pieces cur_player){
                //check lines
                boolean find_kills = true;
                for(int i = 0; i < width; i++){
                        if(getPos(i,src_pos.y) != cur_player){
                                find_kills = false;
                                break;
                        }
                }
                if(find_kills){
                        return 2;
                }
                find_kills = true;
                //check column 
                for(int i = 0; i < height; i++){
                        if(getPos(src_pos.x, i) != cur_player){
                                find_kills = false;
                                break;
                        }
                }
                if(find_kills){
                        return 2;
                }
                //check square
                if(src_pos.x > 0 && src_pos.y > 0){
                        if(getPos(src_pos.x, src_pos.y -1) == cur_player&&
                                getPos(src_pos.x -1, src_pos.y) == cur_player&&
                                getPos(src_pos.x -1, src_pos.y -1) == cur_player){

                                return 1;
                        }

                }
                if(src_pos.x > 0 && src_pos.y < height - 1){
                        if(getPos(src_pos.x -1, src_pos.y) == cur_player&&
                                getPos(src_pos.x , src_pos.y + 1) == cur_player&&
                                getPos(src_pos.x - 1, src_pos.y +1) == cur_player){

                                return 1;
                        }
                }
                if(src_pos.x < width -1 && src_pos.y > 0){
                        if(getPos(src_pos.x , src_pos.y -1) == cur_player&&
                                getPos(src_pos.x + 1 , src_pos.y) == cur_player&&
                                getPos(src_pos.x -1, src_pos.y -1) == cur_player){

                                return 1;
                        }
                }
                if(src_pos.x < width -1 && src_pos.y < height - 1){
                        if(getPos(src_pos.x + 1 , src_pos.y) == cur_player&&
                                getPos(src_pos.x  , src_pos.y + 1) == cur_player&&
                                getPos(src_pos.x +1, src_pos.y +1) == cur_player){
                                return 1;
                        }
                }
                return 0;
        }
        public boolean killPieces(Point pos, Pieces who, GameStatus status){
                if(getPos(pos) != who)
                        return false;
                if(status == GameStatus.GAME_SET || status == GameStatus.GAME_SET_LAST)
                    setPos(pos, Pieces.FORBID);
                else
                    setPos(pos, Pieces.EMPTY);

                return true;
        }
        public boolean haveMoves(int x, int y){
                Point pos = new Point();
                pos.x = x;
                pos.y = y;
                return haveMoves(pos);
        }
        public boolean haveMoves(Point src_pos){
                if(src_pos.y > 0){
                        if(getPos(src_pos.x , src_pos.y - 1) == Pieces.EMPTY)
                                return true;
                }
                if(src_pos.x > 0){
                        if(getPos(src_pos.x - 1, src_pos.y) == Pieces.EMPTY)
                                return true;
                }
                if(src_pos.y < height -1){
                        if(getPos(src_pos.x , src_pos.y + 1) == Pieces.EMPTY)
                                return true;
                }
                if(src_pos.x < height -1){
                        if(getPos(src_pos.x + 1, src_pos.y) == Pieces.EMPTY)
                                return true;
                }
                return false;
        }
        public boolean noMoves(Pieces cur_player){
                boolean find_kills = false;
                for(int i = 0; i < this.width; i++){
                        for(int j = 0; j < this.height; j++){
                                if(pieces[i][j] == cur_player)
                                        if(haveMoves(i, j)){
                                                find_kills = true;
                                                break;
                                        }
                                }
                }
                return find_kills;
        }
        public void cleanForbid(){
                for(int i = 0; i < this.width; i++){
                        for(int j = 0; j < this.height; j++){
                                if(pieces[i][j] == Pieces.FORBID)
                                        pieces[i][j] = Pieces.EMPTY;
                                }
                }
        }
        public void draw(){
                for(int i = 0; i < this.width; i++){
                        for(int j = 0; j < this.height; j++){
                                if(pieces[i][j] == Pieces.EMPTY)
                                        System.out.print("( " + i + " " + j + " *) ");
                                if(pieces[i][j] == Pieces.P1)
                                        System.out.print("( " + i + " " + j + " @) ");
                                if(pieces[i][j] == Pieces.P2)
                                        System.out.print("( " + i + " " + j + " #) ");
                                if(pieces[i][j] == Pieces.FORBID)
                                        System.out.print("( " + i + " " + j + " $) ");
                        }
                        System.out.println("");
                        System.out.println("");
                }
        } 
}
